package com.admin.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.admin.bean.AdminSpBean;
import com.admin.bean.AdminSpCpBean;
import com.admin.bean.AdminSpCpSearchBean;
import com.admin.bean.AdminStatFeeBean;
import com.admin.bean.AdminUserBean;
import com.admin.bean.CompanyBean;
import com.admin.bean.ProvinceBean;
import com.admin.bean.ProvinceCheckedBean;
import com.admin.service.AdminSpCpService;
import com.admin.service.AdminSpService;
import com.admin.service.AdminStatFeeService;
import com.admin.service.AdminUserService;
import com.admin.service.CompanyService;
import com.admin.service.ProvinceService;
import com.admin.service.SpService;
import com.admin.util.BaseUtil;
import com.admin.util.BillingUtil;
import com.admin.util.DateUtil;
import com.admin.util.PageBean;
import com.admin.util.SqlUtil;
import com.admin.util.db.DbOperation;

@Controller
@RequestMapping("/sp")
public class SpAction {

	@Autowired private SpService spService;
	@Autowired private AdminSpService adminSpService;
	@Autowired private CompanyService companyService;
	@Autowired private AdminUserService adminUserService;
	@Autowired private AdminSpCpService adminSpCpService;
	@Autowired private AdminStatFeeService adminStatFeeService;
	
	
	@RequestMapping("/toadd")
	public String toadd(HttpServletRequest request){
		List<CompanyBean> list = companyService.findAllList();
		request.setAttribute("list", list);
		return "spAdd";
	}
	
	
	@RequestMapping("/tofenpei/{spId}")
	public String tofenpei(@PathVariable int spId,HttpServletRequest request) throws IOException{
		//int result = adminSpService.delAdminSp(spId);
		AdminSpBean spBean = adminSpService.findAdminSpById(spId);
		
		List<AdminSpCpBean> spcpList = adminSpCpService.findListBySpId(spId);
		
		StringBuilder sb = new StringBuilder();
		
		for (AdminSpCpBean adminSpCpBean : spcpList) {
			sb.append(adminSpCpBean.getSpcommand()+"<br/>");
		}
		//spBean.getSpcommand();
		List<AdminUserBean> adminUserList = adminUserService.findAll(0, 0);
		//System.out.println(sb.toString());
		request.setAttribute("command", sb.toString());
		request.setAttribute("spBean", spBean);
		request.setAttribute("adminUserList", adminUserList);
		
		return "tofenpei";
	}
	
	@RequestMapping("/fenpei")
	public ModelAndView fenpei(HttpServletRequest request,HttpServletResponse response) throws IOException{
		int spid = BaseUtil.stringToInt(request.getParameter("spid"));
		int cpid = BaseUtil.stringToInt(request.getParameter("cpid"));
		String spcode = request.getParameter("spcode");
		String spcommand = request.getParameter("spcommand");
		int spfee = BaseUtil.stringToInt(request.getParameter("spfee"));
		int spisp = BaseUtil.stringToInt(request.getParameter("spisp"));
		String spprovince = request.getParameter("spprovince");
		
		AdminSpCpBean bean = new AdminSpCpBean();
		bean.setCpid(cpid);
		bean.setSpcode(spcode);
		bean.setSpid(spid);
		bean.setSpcommand(spcommand);
		bean.setSpfee(spfee);
		bean.setSpisp(spisp);
		bean.setSpprovince(spprovince);
		
		AdminSpBean spBean = adminSpService.findAdminSpById(spid);
		
		if(spBean.getSpstatus()==1) {	//精确可用
			spBean.setSpstatus(3);	//则变为精确已分配
			
			adminSpService.updateAdminSp(spBean);
		}
		
		int result = adminSpCpService.adAdminSpCpBean(bean);
		response.setContentType("text/html");
		response.getWriter().print(result>0?"1":"0");
		return null;
	}
	
	@RequestMapping("/list")
	public String list(HttpServletRequest request){
		StringBuffer url = new StringBuffer("?1=1");
		int companyId = BaseUtil.stringToInt(request.getParameter("companyId"),-1);
		
		String spname = request.getParameter("spname");
		String bussiness = request.getParameter("bussiness");
		String spcode = request.getParameter("spcode");
		String spcommand = request.getParameter("spcommand");
		
		StringBuilder condition = new StringBuilder(" 1=1 ");
		if(companyId>0) {
			url.append("&companyId="+companyId);
			condition.append(" and companyid = ");
			condition.append(companyId);
		}
		
		if(spname!=null&&spname.length()>0) {
			condition.append(" and spname like '%");
			condition.append(spname);
			condition.append("%' ");
		}
		
		if(spcommand!=null&&spcommand.length()>0) {
			condition.append(" and spcommand like '%");
			condition.append(spcommand);
			condition.append("%' ");
		}
		
		if(bussiness!=null&&bussiness.length()>0) {
			condition.append(" and bussiness like '%");
			condition.append(bussiness);
			condition.append("%' ");
		}
		
		if(spcode!=null&&spcode.length()>0) {
			condition.append(" and spcode like '%");
			condition.append(spcode);
			condition.append("%' ");
		}
		
		
		List<CompanyBean> companyList = companyService.findAllList();
		request.setAttribute("companyList", companyList);
		
		
		
		
		
		int totalCount = adminSpService.findCount(condition.toString());
		
		//if(companyId>0) {
			//totalCount = adminSpService.findCountByCompanyId(companyId);
			
			int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
			PageBean<AdminSpBean> pageBean = new PageBean<AdminSpBean>(totalCount, currentPage,10);
			List<AdminSpBean> spList = adminSpService.findList(condition.toString(), pageBean.getStart(), pageBean.getPageSize());//findListByCompanyId(pageBean.getStart(), pageBean.getPageSize(), companyId);
			
			List<AdminSpBean> resultList = new ArrayList<AdminSpBean>();
			
			for (AdminSpBean adminSpBean : spList) {
				CompanyBean companyBean = companyService.findCompanyById(adminSpBean.getCompanyid());
				
				adminSpBean.setCompanyname(companyBean.getCompanyName());
				
				resultList.add(adminSpBean);
				
			}
			
			
			pageBean.setUrl(url.toString());
			pageBean.setResult(resultList);
			request.setAttribute("pageBean", pageBean);
//		} else {
//			totalCount = adminSpService.findCount();
//			int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
//			PageBean<AdminSpBean> pageBean = new PageBean<AdminSpBean>(totalCount, currentPage,10);
//			List<AdminSpBean> spList = adminSpService.findList(pageBean.getStart(), pageBean.getPageSize());
//			
//			List<AdminSpBean> resultList = new ArrayList<AdminSpBean>();
//			
//			for (AdminSpBean adminSpBean : spList) {
//				CompanyBean companyBean = companyService.findCompanyById(adminSpBean.getCompanyid());
//				
//				adminSpBean.setCompanyname(companyBean.getCompanyName());
//				
//				resultList.add(adminSpBean);
//				
//			}
//			
//			
//			
//			
//			pageBean.setUrl(url.toString());
//			pageBean.setResult(resultList);
//			request.setAttribute("pageBean", pageBean);
//		}
		
		return "spList";
	}
	
	
	@RequestMapping("/spProvinceList")
	public String spProvinceList(HttpServletRequest request){
		StringBuffer url = new StringBuffer("?1=1");
		int companyId = BaseUtil.stringToInt(request.getParameter("companyId"),-1);
		
		List<CompanyBean> companyList = companyService.findAllList();
		request.setAttribute("companyList", companyList);
		
		
		int totalCount = 0;
		if(companyId>0) {
			totalCount = adminSpService.findCountByCompanyId(companyId);
			
			int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
			PageBean<AdminSpBean> pageBean = new PageBean<AdminSpBean>(totalCount, currentPage,20);
			List<AdminSpBean> spList = adminSpService.findListByCompanyId(pageBean.getStart(), pageBean.getPageSize(), companyId);
			pageBean.setUrl(url.toString());
			pageBean.setResult(spList);
			request.setAttribute("pageBean", pageBean);
		} else {
			totalCount = adminSpService.findCount(" spstatus>0");
			int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
			PageBean<AdminSpBean> pageBean = new PageBean<AdminSpBean>(totalCount, currentPage,20);
			List<AdminSpBean> spList = adminSpService.findList("spstatus>0",pageBean.getStart(), pageBean.getPageSize());
			
			List<AdminSpBean> resultList = new ArrayList<AdminSpBean>();
			
			for (AdminSpBean adminSpBean : spList) {
				CompanyBean companyBean = companyService.findCompanyById(adminSpBean.getCompanyid());
				
				adminSpBean.setCompanyname(companyBean.getCompanyName());
				
				resultList.add(adminSpBean);
				
			}
			
			
			
			
			pageBean.setUrl(url.toString());
			pageBean.setResult(resultList);
			request.setAttribute("pageBean", pageBean);
		}
		
		return "spProvinceList";
	}
	
	
	@RequestMapping("/statList")
	public String statList(HttpServletRequest request){
		StringBuffer url = new StringBuffer("?1=1");
		//int companyId = BaseUtil.stringToInt(request.getParameter("companyId"),-1);
		
		List<CompanyBean> companyList = companyService.findAllList();
		request.setAttribute("companyList", companyList);
		
		List<AdminUserBean> cpList = adminUserService.findCp();
		
		request.setAttribute("cpList", cpList);
		
		request.setAttribute("provinceList",ProvinceService.getProvinceList());
		
		Date date = new Date();
		date.setDate(date.getDate()-1);
		
		StringBuilder condition = new StringBuilder(" where ");
		String startTime = request.getParameter("txtDateStart");
		if(startTime==null||startTime.length()==0||startTime.equals("开始时间")) {
			startTime = DateUtil.formatDate(date);
		}
		condition.append(" dt >= '"+startTime+"'");
		url.append("&txtDateStart="+startTime);
		
		String endTime = request.getParameter("txtDateEnd");
		if(endTime==null||endTime.length()==0||endTime.equals("结束时间")) {
			endTime = DateUtil.formatDate(date);
		}
		condition.append(" and dt <= '"+endTime+"'");
		url.append("&txtDateEnd="+endTime);
		int spId = BaseUtil.stringToInt(request.getParameter("spId"));
		url.append("&spId="+spId);
		if(spId>0) {
			condition.append(" and gatewayid = "+spId);
		} else {
			int companyId = BaseUtil.stringToInt(request.getParameter("companyId"));
			url.append("&companyId="+companyId);
			if(companyId>0) {
				List<AdminSpBean> spList = adminSpService.findListByCompanyId(0, 1000, companyId);
				
				int count = spList.size();
				if(count>0) {
					int i = 0;
					condition.append(" and gatewayid in (");
					for (AdminSpBean adminSpBean : spList) {
						i++;
						condition.append(adminSpBean.getId());
						if(i<count) {
							condition.append(",");
						} else {
							condition.append(")");
						}
					}
				}
				
			}
			
		}
		
		
		int cpId = BaseUtil.stringToInt(request.getParameter("cpId"));
		url.append("&cpId="+cpId);
		if(cpId>0) {
			condition.append(" and channel = "+cpId);
		}
		
		int provinceId = BaseUtil.stringToInt(request.getParameter("provinceId"));
		url.append("&provinceId="+provinceId);
		if(provinceId>0) {
			condition.append(" and province = "+provinceId);
		}
		
		int yunyinshang = BaseUtil.stringToInt(request.getParameter("yunyinshang"));
		url.append("&yunyinshang="+yunyinshang);
		if(yunyinshang>0) {
			condition.append(" and yunyinshang = "+yunyinshang);
		}
		
		condition.append(" group by gatewayid,province,channel,dt,yunyinshang order by dt desc");
		
		
		List<AdminStatFeeBean> statList = adminStatFeeService.findStatByCondition(condition.toString());
		
		int allMo = 0;
		int allCpMo = 0;
		int allMt = 0;
		int allCpMt = 0;
		int allInfofee = 0;
		int allCpInfofee = 0;
		int allInfocome = 0;
		int allCpInfocome = 0;
		int allStatmt = 0;
		int allCpStatmt = 0;
		int allStatfee = 0;
		int allCpStatfee = 0;
		int allStatincome = 0;
		int allCpStatincome = 0;
		
		AdminStatFeeBean all = new AdminStatFeeBean();
		
		for (AdminStatFeeBean adminStatFeeBean : statList) {
			//AdminSpBean spBean = adminSpService.findAdminSpById(adminStatFeeBean.getSpid());
			//AdminUserBean cpBean = adminUserService.findById(adminStatFeeBean.getCpid());
			allCpMo = adminStatFeeBean.getCpMo()+allCpMo;
			//adminStatFeeBean.setCpMo(adminStatFeeBean.getMo()*(100-cpBean.getCppercent())/100);
			allMo = adminStatFeeBean.getMo()+allMo;
			allCpMt = adminStatFeeBean.getCpMt()+allCpMt;
			//adminStatFeeBean.setCpMt(adminStatFeeBean.getMt()*(100-cpBean.getCppercent())/100);
			allMt = adminStatFeeBean.getMt()+allMt;
			allInfofee = adminStatFeeBean.getInfofee()+allInfofee;
			//allCpInfofee = adminStatFeeBean.getStatmt()*
			allInfocome = adminStatFeeBean.getInfocome() + allInfocome;
			//int cpStatmt = adminStatFeeBean.getCpstatmt()*(100-cpBean.getCppercent())/100;
			allCpStatmt = adminStatFeeBean.getCpStatmt() + allCpStatmt;
			//adminStatFeeBean.setCpStatmt(adminStatFeeBean.getStatmt()*(100-cpBean.getCppercent())/100);
			allStatmt = adminStatFeeBean.getStatmt() + allStatmt;
			allStatfee = adminStatFeeBean.getStatfee() + allStatfee;
			allCpStatfee = allCpStatfee + adminStatFeeBean.getCpStatfee();
			//adminStatFeeBean.setCpStatfee(cpStatmt * spBean.getSpfee());
			allStatincome = adminStatFeeBean.getStatincome() + allStatincome;
			
			allCpStatincome = allCpStatincome + adminStatFeeBean.getCpStatincome();
			
			//adminStatFeeBean.setCpStatincome(adminStatFeeBean.getStatmt() * spBean.getSpfee()*(100-cpBean.getCppercent())*85/10000);
		}
		
		all.setMo(allMo);
		all.setCpMo(allCpMo);
		all.setMt(allMt);
		all.setCpMt(allCpMt);
		all.setInfofee(allInfofee);
		all.setInfocome(allInfocome);
		all.setStatmt(allStatmt);
		all.setCpStatmt(allCpStatmt);
		all.setStatfee(allStatfee);
		all.setCpStatfee(allCpStatfee);
		all.setStatincome(allStatincome);
		all.setCpStatincome(allCpStatincome);
		
		request.setAttribute("all", all);
		
		
		int totalCount = statList.size();
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		PageBean<AdminStatFeeBean> pageBean = new PageBean<AdminStatFeeBean>(totalCount, currentPage,1000);
		
		
		
		List<AdminStatFeeBean> resultStatList = BillingUtil.getPageList(pageBean.getCurrentPage(), pageBean.getPageSize(), statList);
		
		for (AdminStatFeeBean adminStatFeeBean : resultStatList) {
			AdminSpBean spBean = adminSpService.findAdminSpById(adminStatFeeBean.getSpid());
			if(spBean!=null) {
				adminStatFeeBean.setSpcode(spBean.getSpcode());
				adminStatFeeBean.setSpcommand(spBean.getSpcommand());
				adminStatFeeBean.setIsp(spBean.getSpisp());
				adminStatFeeBean.setCityname(ProvinceService.getProvince(adminStatFeeBean.getCityid()).getProvince());
				
				AdminUserBean userBean = adminUserService.findById(adminStatFeeBean.getCpid());
				
				if(userBean!=null){
					if(userBean.getCpname()!=null){						
						adminStatFeeBean.setCpname(userBean.getCpname());
					}else{
						adminStatFeeBean.setCpname("");
					}
				}else{
					adminStatFeeBean.setCpname("");
				}
			}
			
			
			
		}
		
		
		pageBean.setResult(resultStatList);
		
		pageBean.setUrl(url.toString());
		request.setAttribute("pageBean", pageBean);
		return "statList";
	}
	
	
	
	@RequestMapping("/hourStatList")
	public String hourStatList(HttpServletRequest request){
		StringBuffer url = new StringBuffer("?1=1");
		//int companyId = BaseUtil.stringToInt(request.getParameter("companyId"),-1);
		
		List<CompanyBean> companyList = companyService.findAllList();
		request.setAttribute("companyList", companyList);
		
		List<AdminUserBean> cpList = adminUserService.findCp();
		
		request.setAttribute("cpList", cpList);
		
		request.setAttribute("provinceList",ProvinceService.getProvinceList());
		
		Date date = new Date();
		
		StringBuilder condition = new StringBuilder(" where ");
		
		String startTime = request.getParameter("txtDateStart");
		if(startTime==null||startTime.length()==0||startTime.equals("选择日期")) {
			startTime = DateUtil.formatDate(date);
		}
		url.append("&txtDateStart="+startTime);
		//String startTime = DateUtil.formatDate(date);
		condition.append(" dt like '%"+startTime+"%'");
		
		int spId = BaseUtil.stringToInt(request.getParameter("spId"));
		url.append("&spId="+spId);
		if(spId>0) {
			condition.append(" and gatewayid = "+spId);
		} else {
			int companyId = BaseUtil.stringToInt(request.getParameter("companyId"));
			url.append("&companyId="+companyId);
			if(companyId>0) {
				List<AdminSpBean> spList = adminSpService.findListByCompanyId(0, 1000, companyId);
				
				int count = spList.size();
				if(count>0) {
					int i = 0;
					condition.append(" and gatewayid in (");
					for (AdminSpBean adminSpBean : spList) {
						i++;
						condition.append(adminSpBean.getId());
						if(i<count) {
							condition.append(",");
						} else {
							condition.append(")");
						}
					}
				}
				
			}
			
		}
		
		
		int cpId = BaseUtil.stringToInt(request.getParameter("cpId"));
		url.append("&cpId="+cpId);
		if(cpId>0) {
			condition.append(" and channel = "+cpId);
		}
		
		int provinceId = BaseUtil.stringToInt(request.getParameter("provinceId"));
		url.append("&provinceId="+provinceId);
		if(provinceId>0) {
			condition.append(" and province = "+provinceId);
		}
		
		condition.append(" group by gatewayid,province,channel,dt order by dt desc");
		
		//System.out.println(condition.toString());
		List<AdminStatFeeBean> statList = adminStatFeeService.findHourStatByCondition(condition.toString());
		
		int allMo = 0;
		int allCpMo = 0;
		int allMt = 0;
		int allCpMt = 0;
		int allInfofee = 0;
		int allCpInfofee = 0;
		int allInfocome = 0;
		int allCpInfocome = 0;
		int allStatmt = 0;
		int allCpStatmt = 0;
		int allStatfee = 0;
		int allCpStatfee = 0;
		int allStatincome = 0;
		int allCpStatincome = 0;
		
		AdminStatFeeBean all = new AdminStatFeeBean();
		
		for (AdminStatFeeBean adminStatFeeBean : statList) {
			allCpMo = adminStatFeeBean.getCpMo()+allCpMo;
			allMo = adminStatFeeBean.getMo()+allMo;
			allCpMt = adminStatFeeBean.getCpMt()+allCpMt;
			allMt = adminStatFeeBean.getMt()+allMt;
			allInfofee = adminStatFeeBean.getInfofee()+allInfofee;
			//allCpInfofee = adminStatFeeBean.getStatmt()*
			allInfocome = adminStatFeeBean.getInfocome() + allInfocome;
			allCpStatmt = adminStatFeeBean.getCpStatmt() + allCpStatmt;
			allStatmt = adminStatFeeBean.getStatmt() + allStatmt;
			allStatfee = adminStatFeeBean.getStatfee() + allStatfee;
			allCpStatfee = allCpStatfee + adminStatFeeBean.getCpStatfee();
			allStatincome = adminStatFeeBean.getStatincome() + allStatincome;
			
			allCpStatincome = allCpStatincome + adminStatFeeBean.getCpStatincome();
		}
		
		all.setMo(allMo);
		all.setCpMo(allCpMo);
		all.setMt(allMt);
		all.setCpMt(allCpMt);
		all.setInfofee(allInfofee);
		all.setInfocome(allInfocome);
		all.setStatmt(allStatmt);
		all.setCpStatmt(allCpStatmt);
		all.setStatfee(allStatfee);
		all.setCpStatfee(allCpStatfee);
		all.setStatincome(allStatincome);
		all.setCpStatincome(allCpStatincome);
		
		request.setAttribute("all", all);
		
		
		int totalCount = statList.size();
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		PageBean<AdminStatFeeBean> pageBean = new PageBean<AdminStatFeeBean>(totalCount, currentPage,20);
		
		
		
		List<AdminStatFeeBean> resultStatList = BillingUtil.getPageList(pageBean.getCurrentPage(), pageBean.getPageSize(), statList);
		
		for (AdminStatFeeBean adminStatFeeBean : resultStatList) {
			AdminSpBean spBean = adminSpService.findAdminSpById(adminStatFeeBean.getSpid());
			if(spBean!=null) {
				adminStatFeeBean.setSpcode(spBean.getSpcode());
				adminStatFeeBean.setSpcommand(spBean.getSpcommand());
				adminStatFeeBean.setIsp(spBean.getSpisp());
				adminStatFeeBean.setCityname(ProvinceService.getProvince(adminStatFeeBean.getCityid()).getProvince());
				
				AdminUserBean userBean = adminUserService.findById(adminStatFeeBean.getCpid());
				
				adminStatFeeBean.setCpname(userBean.getCpname());
			}
			
		}
		
		
		pageBean.setResult(resultStatList);
		
		pageBean.setUrl(url.toString());
		request.setAttribute("pageBean", pageBean);
		
		return "hourStatList";
	}
	
	
	@RequestMapping("/cpStatList")
	public String cpStatList(HttpServletRequest request){
		
		AdminUserBean user = (AdminUserBean) request.getSession().getAttribute("adminUser");
		
		StringBuffer url = new StringBuffer("?1=1");
		//int companyId = BaseUtil.stringToInt(request.getParameter("companyId"),-1);
		
		List<CompanyBean> companyList = companyService.findAllList();
		request.setAttribute("companyList", companyList);
		
		List<AdminSpCpBean> spCpList = adminSpCpService.findListByCpId(user.getId());
		
		request.setAttribute("spCpList", spCpList);
		
		
		request.setAttribute("provinceList",ProvinceService.getProvinceList());
		
		Date date = new Date();
		date.setDate(date.getDate()-1);
		
		StringBuilder condition = new StringBuilder(" where ");
		String startTime = request.getParameter("txtDateStart");
		if(startTime==null||startTime.length()==0||startTime.equals("开始时间")) {
			startTime = DateUtil.formatDate(date);
		}
		condition.append(" dt >= '"+startTime+"'");
		url.append("&txtDateStart="+startTime);
		
		String endTime = request.getParameter("txtDateEnd");
		if(endTime==null||endTime.length()==0||endTime.equals("结束时间")) {
			endTime = DateUtil.formatDate(date);
		}
		condition.append(" and dt <= '"+endTime+"'");
		url.append("&txtDateEnd="+endTime);
		int spId = BaseUtil.stringToInt(request.getParameter("spId"));
		url.append("&spId="+spId);
		if(spId>0) {
			condition.append(" and gatewayid = "+spId);
		} else {
			int companyId = BaseUtil.stringToInt(request.getParameter("companyId"));
			url.append("&companyId="+companyId);
			if(companyId>0) {
				List<AdminSpBean> spList = adminSpService.findListByCompanyId(0, 1000, companyId);
				
				int count = spList.size();
				if(count>0) {
					int i = 0;
					condition.append(" and gatewayid in (");
					for (AdminSpBean adminSpBean : spList) {
						i++;
						condition.append(adminSpBean.getId());
						if(i<count) {
							condition.append(",");
						} else {
							condition.append(")");
						}
					}
				}
				
			}
			
		}
		
		
		int cpId = user.getId();
		
		AdminUserBean cpBean = adminUserService.findById(cpId);
		
		request.setAttribute("cpBean", cpBean);
		
		condition.append(" and channel = "+cpId);
		
		
		int provinceId = BaseUtil.stringToInt(request.getParameter("provinceId"));
		url.append("&provinceId="+provinceId);
		if(provinceId>0) {
			condition.append(" and province = "+provinceId);
		}
		
		condition.append(" group by gatewayid,province,channel,dt order by dt desc");
		
		
		List<AdminStatFeeBean> statList = adminStatFeeService.findStatByCondition(condition.toString());
		
		int allMo = 0;
		int allCpMo = 0;
		int allMt = 0;
		int allCpMt = 0;
		int allInfofee = 0;
		int allCpInfofee = 0;
		int allInfocome = 0;
		int allCpInfocome = 0;
		int allStatmt = 0;
		int allCpStatmt = 0;
		int allStatfee = 0;
		int allCpStatfee = 0;
		int allStatincome = 0;
		int allCpStatincome = 0;
		
		AdminStatFeeBean all = new AdminStatFeeBean();
		
		for (AdminStatFeeBean adminStatFeeBean : statList) {
			
			allCpMo = adminStatFeeBean.getCpMo()+allCpMo;
			//adminStatFeeBean.setCpMo(adminStatFeeBean.getMo()*(100-cpBean.getCppercent())/100);
			allMo = adminStatFeeBean.getMo()+allMo;
			allCpMt = adminStatFeeBean.getCpMt()+allCpMt;
			//adminStatFeeBean.setCpMt(adminStatFeeBean.getMt()*(100-cpBean.getCppercent())/100);
			allMt = adminStatFeeBean.getMt()+allMt;
			allInfofee = adminStatFeeBean.getInfofee()+allInfofee;
			//allCpInfofee = adminStatFeeBean.getStatmt()*
			allInfocome = adminStatFeeBean.getInfocome() + allInfocome;
			//int cpStatmt = adminStatFeeBean.getCpstatmt()*(100-cpBean.getCppercent())/100;
			allCpStatmt = adminStatFeeBean.getCpStatmt() + allCpStatmt;
			//adminStatFeeBean.setCpStatmt(adminStatFeeBean.getStatmt()*(100-cpBean.getCppercent())/100);
			allStatmt = adminStatFeeBean.getStatmt() + allStatmt;
			allStatfee = adminStatFeeBean.getStatfee() + allStatfee;
			allCpStatfee = allCpStatfee + adminStatFeeBean.getCpStatfee();
			//adminStatFeeBean.setCpStatfee(cpStatmt * spBean.getSpfee());
			allStatincome = adminStatFeeBean.getStatincome() + allStatincome;
			
			allCpStatincome = allCpStatincome + adminStatFeeBean.getCpStatincome();
		}
		
		all.setMo(allMo);
		all.setCpMo(allCpMo);
		all.setMt(allMt);
		all.setCpMt(allCpMt);
		all.setInfofee(allInfofee);
		all.setInfocome(allInfocome);
		all.setStatmt(allStatmt);
		all.setCpStatmt(allCpStatmt);
		all.setStatfee(allStatfee);
		all.setCpStatfee(allCpStatfee);
		all.setStatincome(allStatincome);
		all.setCpStatincome(allCpStatincome);
		
		request.setAttribute("all", all);
		
		
		int totalCount = statList.size();
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		PageBean<AdminStatFeeBean> pageBean = new PageBean<AdminStatFeeBean>(totalCount, currentPage,10);
		
		
		
		List<AdminStatFeeBean> resultStatList = BillingUtil.getPageList(pageBean.getCurrentPage(), pageBean.getPageSize(), statList);
		
		for (AdminStatFeeBean adminStatFeeBean : resultStatList) {
			AdminSpBean spBean = adminSpService.findAdminSpById(adminStatFeeBean.getSpid());
			adminStatFeeBean.setSpcode(spBean.getSpcode());
			adminStatFeeBean.setSpcommand(spBean.getSpcommand());
			adminStatFeeBean.setIsp(spBean.getSpisp());
			adminStatFeeBean.setCityname(ProvinceService.getProvince(adminStatFeeBean.getCityid()).getProvince());
			
			AdminUserBean userBean = adminUserService.findById(adminStatFeeBean.getCpid());
			
			adminStatFeeBean.setCpname(userBean.getCpname());
			
			
			
		}
		
		
		pageBean.setResult(resultStatList);
		
		pageBean.setUrl(url.toString());
		request.setAttribute("pageBean", pageBean);
		return "cpStatList";
	}
	
	
	@RequestMapping("/spcplist")
	public String spcplist(HttpServletRequest request){
		StringBuffer url = new StringBuffer("?1=1");
		int cpid = BaseUtil.stringToInt(request.getParameter("cpid"));
		String spcode = request.getParameter("spcode");
		String spcommand = request.getParameter("spcommand");
		String spname = request.getParameter("spname");
		
		StringBuilder condition = new StringBuilder(" 1=1 ");
		
		if(cpid>0) {
			condition.append(" and sc.cpid = ");
			condition.append(cpid);
		}
		if(spcode!=null&&spcode.length()>0) {
			condition.append(" and sc.spcode like '%");
			condition.append(spcode);
			condition.append("%' ");
		}
		
		if(spcommand!=null&&spcommand.length()>0) {
			condition.append(" and sc.spcommand like '%");   
			condition.append(spcommand);
			condition.append("%' ");
		}
		
		int totalCount = adminSpCpService.findCountBySpCp(condition.toString().replaceAll("sc.", ""));
		
		if(spname!=null&&spname.length()>0) {
			condition.append(" and sp.spname like '%");
			condition.append(spname);
			condition.append("%' ");
		}
		
		
		List<AdminUserBean> cpList = adminUserService.findCp();
		
		request.setAttribute("cpList", cpList);
		
		
		
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		
		PageBean<AdminSpCpSearchBean> pageBean = new PageBean<AdminSpCpSearchBean>(totalCount, currentPage,10);
		
		List<AdminSpCpSearchBean> searchList = adminSpCpService.findListBySpCp(pageBean.getStart(), pageBean.getPageSize(), condition.toString());
		
		for (AdminSpCpSearchBean adminSpCpSearchBean : searchList) {
			AdminSpBean spBean = adminSpService.findAdminSpById(adminSpCpSearchBean.getSpid());
			adminSpCpSearchBean.setShildcity(spBean.getShieldcity());
			adminSpCpSearchBean.setSecond(spBean.getSpmemo());
		}
		
		
		pageBean.setUrl(url.toString());
		pageBean.setResult(searchList);
		request.setAttribute("pageBean", pageBean);
		
//		if(companyId>0) {
//			totalCount = adminSpService.findCountByCompanyId(companyId);
//			
//			int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
//			PageBean<AdminSpBean> pageBean = new PageBean<AdminSpBean>(totalCount, currentPage,10);
//			List<AdminSpBean> spList = adminSpService.findListByCompanyId(pageBean.getStart(), pageBean.getPageSize(), companyId);
//			pageBean.setUrl(url.toString());
//			pageBean.setResult(spList);
//			request.setAttribute("pageBean", pageBean);
//		} else {
//			totalCount = adminSpService.findCount();
//			int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
//			PageBean<AdminSpBean> pageBean = new PageBean<AdminSpBean>(totalCount, currentPage,10);
//			List<AdminSpBean> spList = adminSpService.findList(pageBean.getStart(), pageBean.getPageSize());
//			pageBean.setUrl(url.toString());
//			pageBean.setResult(spList);
//			request.setAttribute("pageBean", pageBean);
//		}
		
		return "spcpList";
	}
	//查看单个CP的数据
	@RequestMapping("/selectById")
	public String selectById(HttpServletRequest request,HttpServletResponse response) throws IOException{
		int cpid = BaseUtil.stringToInt(request.getParameter("id"));
		AdminSpCpBean selectById = adminSpCpService.findAdminSpCpBean(cpid);
		request.setAttribute("selectById", selectById);
		return "selectById";
	}
	//修改单个CP的数据的扣量字段
	@RequestMapping("/updatekouliang")
	public ModelAndView updatekouliang(HttpServletRequest request,HttpServletResponse response) throws IOException{
		int id = BaseUtil.stringToInt(request.getParameter("id"));
		int kouliang = BaseUtil.stringToInt(request.getParameter("kouliang"));
		System.out.println("---------------------------");
		System.out.println("kouliang:"+kouliang);
		System.out.println("id:"+id);
		int flag= adminSpCpService.updateKouLiang(kouliang,id);
		response.setContentType("text/html");
		response.getWriter().print(flag>0?"1":"0");
		return null;
	}
	
	
	@RequestMapping("/cpOfSpList")
	public String cpOfSplist(HttpServletRequest request){
		
		AdminUserBean user = (AdminUserBean) request.getSession().getAttribute("adminUser");
		
		StringBuffer url = new StringBuffer("?1=1");
		
		
		List<AdminUserBean> cpList = adminUserService.findCp();
		
		request.setAttribute("cpList", cpList);
		
		
		int totalCount = adminSpCpService.findCountBySpCp(" cpid = "+user.getId());
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		
		PageBean<AdminSpCpSearchBean> pageBean = new PageBean<AdminSpCpSearchBean>(totalCount, currentPage,10);
		
		List<AdminSpCpSearchBean> searchList = adminSpCpService.findListBySpCp(pageBean.getStart(), pageBean.getPageSize(), "sc.cpid = "+user.getId());
		
		
		for (AdminSpCpSearchBean adminSpCpSearchBean : searchList) {
			AdminSpBean spBean = adminSpService.findAdminSpById(adminSpCpSearchBean.getSpid());
			adminSpCpSearchBean.setShildcity(spBean.getShieldcity());
			adminSpCpSearchBean.setSecond(spBean.getSpmemo());
		}
		
		pageBean.setUrl(url.toString());
		pageBean.setResult(searchList);
		request.setAttribute("pageBean", pageBean);
		
		return "cpOfSpList";
	}
	
	/**
	 * ����SP
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/add")
	public ModelAndView add(HttpServletRequest request,HttpServletResponse response) throws IOException{
		String spname = request.getParameter("spname");
		String spcode = request.getParameter("spcode");
		String spcodename = request.getParameter("spcodename");
		String bussiness = request.getParameter("bussiness");
		String shieldcity = request.getParameter("shieldcity");
		
		String spcommand = request.getParameter("spcommand");
		//String command = request.getParameter("command");
		int spfee = BaseUtil.stringToInt(request.getParameter("spfee"))*100;
		
		int spisp = BaseUtil.stringToInt(request.getParameter("spisp"));
		String spmt = request.getParameter("spmt");
		int spdaylimit = BaseUtil.stringToInt(request.getParameter("spdaylimit"));
		int spmonthlimit = BaseUtil.stringToInt(request.getParameter("spmonthlimit"));
		String spmemo = request.getParameter("spmemo");
		int spstatus = BaseUtil.stringToInt(request.getParameter("spstatus"));
		int companyid = BaseUtil.stringToInt(request.getParameter("companyid"));
		String spdesc = request.getParameter("spdesc");
		
		AdminSpBean adminSpBean = new AdminSpBean();
		adminSpBean.setCompanyid(companyid);
		adminSpBean.setShieldcity(shieldcity);
		adminSpBean.setSpcodename(spcodename);
		adminSpBean.setSpcode(spcode);
		adminSpBean.setSpcommand(spcommand);
		adminSpBean.setSpdaylimit(spdaylimit);
		adminSpBean.setSpmonthlimit(spmonthlimit);
		adminSpBean.setSpfee(spfee);
		adminSpBean.setSpisp(spisp);
		adminSpBean.setSpmemo(spmemo);
		adminSpBean.setSpmt(spmt);
		adminSpBean.setSpname(spname);
		adminSpBean.setBussiness(bussiness);
		
		adminSpBean.setSpstatus(spstatus);
		
		String[] strs = request.getParameterValues("provinceid");
		StringBuilder sb = new StringBuilder();
		for (String string : strs) {
			sb.append(string+",");
		}
		
		adminSpBean.setSpprovince(sb.toString());
		adminSpBean.setSpdesc(spdesc);
		
		int result = adminSpService.addAdminSp(adminSpBean);
		response.setContentType("text/html");
		response.getWriter().print(result>0?"1":"0");
		return null;
	}
	
	@RequestMapping("/{spId}")
	public String view(@PathVariable int spId,HttpServletRequest request){
		AdminSpBean sp = adminSpService.findAdminSpById(spId);
		String[] strs = sp.getSpprovince().split(",");
		List<Integer> lists = new ArrayList<Integer>();
		for (String string : strs) {
			int provinceId = BaseUtil.stringToInt(string);
			lists.add(provinceId);
		}
		List<ProvinceCheckedBean> list = new ArrayList<ProvinceCheckedBean>();
		for(ProvinceBean province:ProvinceService.getProvinceList()){
			
			boolean flag = false;
			
			for (Integer integer : lists) {
				if(province.getId()==integer) {
					flag = true;
					break;
				}
			}
			if(flag) {
				ProvinceCheckedBean bean = new ProvinceCheckedBean();
				bean.setId(province.getId());
				bean.setStatus(1);
				bean.setProvince(province.getProvince());
				
				list.add(bean);
			} else {
				ProvinceCheckedBean bean = new ProvinceCheckedBean();
				bean.setId(province.getId());
				bean.setStatus(0);
				bean.setProvince(province.getProvince());
				
				list.add(bean);
			}
		}
		
		CompanyBean companyBean = companyService.findCompanyById(sp.getCompanyid());
		
		request.setAttribute("companyBean", companyBean);
		request.setAttribute("list", list);
		request.setAttribute("sp", sp);
		return "spView";
	}
	
	@RequestMapping("/del/{spId}")
	public ModelAndView del(@PathVariable int spId,HttpServletResponse response) throws IOException{
		int result = adminSpService.delAdminSp(spId);
		System.out.println("....");
		response.setContentType("text/html");
		response.getWriter().print(result>0?"1":"0");
		return null;
	}
	
	@RequestMapping("/spcpdel")
	public ModelAndView spcpdel(HttpServletRequest request,HttpServletResponse response) throws IOException{
		int id = BaseUtil.stringToInt(request.getParameter("id"));
		
		AdminSpCpBean adminSpCpBean = adminSpCpService.findAdminSpCpBean(id);
		
		if(adminSpCpBean!=null) {
			AdminSpBean spBean = adminSpService.findAdminSpById(adminSpCpBean.getSpid());
			if(spBean!=null) {
				if(spBean.getSpstatus()==3) {//精确不可用
					spBean.setSpstatus(1);//变为精确可用
					adminSpService.updateAdminSp(spBean);
				}
			}
		}
		
		
		int result = adminSpCpService.delSpCp(id);
		response.setContentType("text/html");
		response.getWriter().print(result>0?"1":"0");
		return null;
	}
	
	
	@RequestMapping("/selectSp")
	public ModelAndView selectSp(HttpServletRequest request,HttpServletResponse response) throws IOException{
		int companyId = BaseUtil.stringToInt(request.getParameter("companyId"));
		
		List<AdminSpBean> list = adminSpService.findListByCompanyId(0, 1000, companyId);
		
		StringBuilder result = new StringBuilder();
		result.append("<option value=\"0\">请选择</option>");
		
		for (AdminSpBean adminSpBean : list) {
			result.append("<option value=\"");
			result.append(adminSpBean.getId());
			result.append("\">");
			result.append(adminSpBean.getSpname());
			result.append("</option>");
		}
		
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().print(result.toString());
		return null;
	}
	
	
	
	
	@RequestMapping("/update")
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response) throws IOException{
		int id = BaseUtil.stringToInt(request.getParameter("id"));
		String spname = request.getParameter("spname");
		String spcodename = request.getParameter("spcodename");
		String bussiness = request.getParameter("bussiness");
		String spcode = request.getParameter("spcode");
		String spcommand = request.getParameter("spcommand");
		String shieldcity = request.getParameter("shieldcity");
		//String command = request.getParameter("command");
		
		System.out.println("spfee:"+request.getParameter("spfee"));
		
		int spfee = 0;
		
		try {
			spfee = Float.valueOf(request.getParameter("spfee")).intValue()*100;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			spfee = 0;
		} finally {

		}
		
		
		
		int spisp = BaseUtil.stringToInt(request.getParameter("spisp"));
		String spmt = request.getParameter("spmt");
		int spdaylimit = BaseUtil.stringToInt(request.getParameter("spdaylimit"));
		int spmonthlimit = BaseUtil.stringToInt(request.getParameter("spmonthlimit"));
		String spmemo = request.getParameter("spmemo");
		int spstatus = BaseUtil.stringToInt(request.getParameter("spstatus"));
		int companyid = BaseUtil.stringToInt(request.getParameter("companyid"));
		String spdesc = request.getParameter("spdesc");
		
		AdminSpBean adminSpBean = new AdminSpBean();
		adminSpBean.setId(id);
		adminSpBean.setSpcodename(spcodename);
		adminSpBean.setShieldcity(shieldcity);
		adminSpBean.setCompanyid(companyid);
		adminSpBean.setSpcode(spcode);
		adminSpBean.setSpcommand(spcommand);
		adminSpBean.setSpdaylimit(spdaylimit);
		adminSpBean.setSpmonthlimit(spmonthlimit);
		adminSpBean.setSpfee(spfee);
		adminSpBean.setSpisp(spisp);
		adminSpBean.setSpmemo(spmemo);
		adminSpBean.setSpmt(spmt);
		adminSpBean.setSpname(spname);
		adminSpBean.setBussiness(bussiness);
		
		adminSpBean.setSpstatus(spstatus);
		adminSpBean.setSpdesc(spdesc);
		
		String[] strs = request.getParameterValues("provinceid");
		StringBuilder sb = new StringBuilder();
		for (String string : strs) {
			sb.append(string+",");
		}
		adminSpBean.setSpprovince(sb.toString());
		int result = adminSpService.updateAdminSp(adminSpBean);
		
		if(result>0) {
			if(adminSpBean.getSpstatus()==0) {
				adminSpCpService.delSpCpBySpId(id);
			}
		}
		
		response.setContentType("text/html");
		response.getWriter().print(result>0?"1":"0");
		return null;
	}
	
	
	@RequestMapping("/recvSend")
	public String recvSend(HttpServletRequest request){
		
		String txtDateStart = request.getParameter("txtDateStart");
		
		String phone = request.getParameter("phone");
		
		String table = DateUtil.formatDate(DateUtil.parseDate(txtDateStart),"yyMMdd");
		
		
		List list = new ArrayList();
		if(phone!=null&&phone.length()==11)
			list = SqlUtil.getObjectsList("select recv.org as org,recv.dest as dest, recv.body as body,send.stat as stat,recv.tm as tm from recv_"+table+" as recv left join send_"+table+" as send on recv.linkid = send.linkid where recv.org = '"+phone+"' order by recv.tm desc", DbOperation.DB_MUZHI);
		
		
		request.setAttribute("list", list);
		
		return "recvSend";
	}
	
}
